<?php
define('PHOTO_DIR', './photos/');
require('lib/model.php');
require('lib/diy.php');
// $model = new DiyModel("address_suggest_sigugun");
// $result = $model->find("all", array("limit" => 10));
$smarty = new Smarty_Diycity();
$problem = new DiyModel("problems");
$update = new DiyModel("updates");

/*
$problem->reset();
$updates->reset();
*/

$recent = $problem->find('all', array('conditions' => array('state !=' => '-1'),
				      'fields' => array('id', 'subject', 'state'),
				      'limit' => 5,
				      'order' => "id desc"));

foreach ($recent as &$item) {
  $cnt = $update->find('count', array('conditions' => array('problem_id' => $item['id'])));
  $item['update'] = $cnt;
}

$week_before_date = add_date(get_current_time(), -7);
$week_count = $problem->find('count', array('conditions' =>
					    array('created_time >' => $week_before_date)));
$total_count = $problem->find('count');


$ids = $problem->find('all', array('conditions' => array('state !=' => '-1'),
				   'fields' => array('id'),
				   'limit' => 30,
				   'order' => "id desc"));

$month_str = date("Y-M");
$month_str .= "%";

// $month_count = $problem->find('count', array('conditions' =>
// 					     array('created_time like' => $month_str,
// 						   'state' => 1)));

$month_count = $problem->find('count', array('conditions' =>
					     array('state' => 1)));

// $partial_count = $problem->find('count', array('conditions' =>
// 					       array('created_time like' => $month_str,
// 						     'state' => 2)));


$partial_count = $problem->find('count', array('conditions' =>
					       array('state' => 2)));


$smarty->assign('partial_count', $partial_count);
$smarty->assign('month_count', $month_count);
$smarty->assign('week_count', $week_count);
$smarty->assign('total_count', $total_count);

$img_list = array();
foreach($ids as $key => $value) {
  if (file_exists(PHOTO_DIR . $value['id'] . '_thumb'))
    $img_list[] = array('id' => $value['id'], 'img' => $value['id']);
  else
    $img_list[] = array('id' => $value['id'], 'img' => 'dummy');

}

if (sizeof($img_list) > 0) {
  $smarty->assign('recent_images', $img_list);
}


if (sizeof($recent) > 0) {
  $smarty->assign('recent_reports', $recent);
}

$smarty->display('index.tpl');
?>
